Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/props in markdown #133

Merged
merged 3 commits into from
Oct 30, 2022
Merged

Feature/props in markdown #133

merged 3 commits into from
Oct 30, 2022

Conversation

helmturner
Copy link
Collaborator

@helmturner helmturner commented Oct 30, 2022

  • Please check if the PR fulfills these requirements
  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

I'll file a separate issue for docs/tests on all features realted to the blog or Markdown/MDX.

  • What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Feature: frontmatter is now injected as pageProps (behind the scenes, this uses getStaticProps).

  • What is the current behavior? (You can also link to an open issue here)

Frontmatter is useless.

  • What is the new behavior (if this is a feature change)?

Front-matter is now available in _app.tsx as pageProps, and can be used for all sorts of neat stuff programmatically.

For example:

// src/pages/_app.tsx

import {MDXProvider} from '@mdx-js/react';
import blogComponents from '../features/blog/components'
import eventComponents from '../features/events/components'

function MyApp({Component, pageProps}: AppPropsWithLayout) {
  let components = {};
  // For `.md` & `.mdx` files in the `pages` directory, frontmatter from the file becomes `pageProps`
  if (pageProps.contentType === 'blog') {
    components = blogComponents
  }
  if (pageProps.contentType === 'event') {
    components = eventComponents
  }
  return (
    <MDXProvider components={components}>
      <Component {...pageProps} />
    </MDXProvider>
  );
}

In addition frontmatter values in .mdx pages can be used as input variables for the MDX on that page. For example, the following MDX file:

---
title: The answer to life, the universe, and everything
answer: 42
---

{title} is {answer}

is roughly equivalent to the following:

export const title = 'The answer to life, the universe, and everything'
export const answer = '42'

export const getStaticProps = () => {
  return {
    props: JSON.parse(
      JSON.stringify({ title, answer })
    ),
  }
}

export default function MDXPage({ title, answer }) {
  return (
    <p>
      {title} is {answer}
    </p>
  )
}
  • Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

Nope!

Next we will need to validate the frontmatter and do stuff with it.

@helmturner helmturner self-assigned this Oct 30, 2022
@helmturner helmturner added enhancement New feature or request back end apis, server logic, plugins hacktoberfest-accepted iykyk and removed hacktoberfest-accepted iykyk labels Oct 30, 2022
@helmturner helmturner merged commit c0ea9eb into dev Oct 30, 2022
@zenlex zenlex deleted the feature/props-in-markdown branch October 30, 2022 23:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
back end apis, server logic, plugins enhancement New feature or request hacktoberfest-accepted iykyk
Projects
Development

Successfully merging this pull request may close these issues.

Feat: Turn front-matter in markdown pages into something useful
2 participants